package com.airtight.objects;

import com.airtight.programs.TextureShaderProgram;

import static android.opengl.Matrix.orthoM;
import static android.opengl.Matrix.translateM;

public class Ball extends Object{
    public final float[] ballMatrix = new float[16];
    private float transparency = 1f;
    public float acceleration = 0.0005f;
    public float velocity = 0;
    public int bounces = 0;
    public boolean down = true;

    public Ball(float x, float y, float width, float height) {
        super(x,y,width,height);
        orthoM(ballMatrix, 0, -1f, 1f, -1, 1, -1, 1f);
    }

    public void MoveSide(float accelx){

        if(accelx < -0.2){
            translateM(ballMatrix, 0, -0.01f*accelx, 0, 0);
        }

        if(accelx > 0.2){
            translateM(ballMatrix, 0, -0.01f*accelx, 0, 0);
        }
    }

    public void Fall(){

        if(velocity <= 0){
            down = true;
        }

        if(down){
            velocity += acceleration;
            translateM(ballMatrix, 0, 0, -velocity, 0);
        }

        else{
            velocity -= acceleration;
            translateM(ballMatrix, 0, 0, velocity, 0);
        }
    }

    public void drawBall(TextureShaderProgram textureProgram, int asset){
        textureProgram.useProgram();
        textureProgram.setTexture(ballMatrix, asset);
        textureProgram.setOpacity(transparency);
        bindData(textureProgram);
        draw();
    }

    public float getY(){
        return ballMatrix[13];
    }

    public void setY(float y){
        ballMatrix[13] = y;
    }

    public float getX(){
        return ballMatrix[12];
    }

    public void setX(float x){
        ballMatrix[12] = x;
    }

    public void resetParams(){
        velocity = 0;
        ballMatrix[12] = 0;
        ballMatrix[13] = 0;
        bounces = 0;
    }
}
